package com.pjbao.Service;

import java.awt.*;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class EWEServices {

    static Logger logger = LoggerFactory.getLogger("api service v1");

    public static RetCode fhConditionQuery(String baseurl, String name, String type, String userid) {

        RetCode ret = new RetCode();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(baseurl + "b401/" + name + "/" + type + "?token=" + userid);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
            ret.setHttp(response.getStatusLine().getStatusCode());
            HttpEntity entity = response.getEntity();
            if (ret.getHttp() == 200) {
                String content = EntityUtils.toString(entity);
                parseContent(ret, content);
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return ret;
    }


    //B402
    public static RetCode b402Query(String baseURL, String idType, String userid) {
//    String baseURL = reportConfig.getIp();
        RetCode ret = new RetCode();

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(baseURL + "b402/" + idType + "?token=" + userid);
        CloseableHttpResponse response = null;
        try {

            response = httpclient.execute(httpGet);
            ret.setHttp(response.getStatusLine().getStatusCode());

            HttpEntity entity = response.getEntity();
            if (ret.getHttp() == 200) {
                String content = EntityUtils.toString(entity);
                parseContent(ret, content);
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return ret;
    }


    public static RetCode ysBusinessCircles(String baseURL, String name, Integer keyType, String userid) {
        RetCode ret = new RetCode();

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(baseURL + "b1/" + name + "/" + keyType + "?token=" + userid);
        CloseableHttpResponse response = null;
        try {

            response = httpclient.execute(httpGet);
            ret.setHttp(response.getStatusLine().getStatusCode());
            HttpEntity entity = response.getEntity();
            if (ret.getHttp() == 200) {
                String content = EntityUtils.toString(entity);
                parseContent(ret, content);
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return ret;
    }


    public static void parseContent(RetCode ret, String content) {
        // parse the msg
        {
            String pattern = "\"msg\":\"([^\"]*)\"";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(content);
            if (m.find()) {
                String msgStr = m.group(0).substring(7, m.group(0).length() - 1);
                ret.setServiceDes(msgStr);
            }
        }
        // parse the code
        {
            String pattern = "\"code\":[\\d-]+";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(content);
            if (m.find()) {
                String codeStr = m.group(0).substring(7);
                ret.setService(Integer.parseInt(codeStr));
            }
            ret.setData(content);
        }
    }

    public static class RetCode {
        Integer http;
        Integer service;
        String serviceDes;

        String data;

        public RetCode(Integer http, Integer service, String serviceDes, String data) {
            super();
            this.http = http;
            this.service = service;
            this.serviceDes = serviceDes;
            this.data = data;
        }


        public RetCode() {
            super();
            http = -1;
            service = -1;
            data = null;
            // TODO Auto-generated constructor stub
        }

        public Integer getHttp() {
            return http;
        }

        public void setHttp(Integer http) {
            this.http = http;
        }

        public Integer getService() {
            return service;
        }

        public void setService(Integer service) {
            this.service = service;
        }

        public String getServiceDes() {
            return serviceDes;
        }

        public void setServiceDes(String serviceDes) {
            this.serviceDes = serviceDes;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }


        @Override
        public String toString() {
            return "RetCode [http=" + http + ", service=" + service + ", serviceDes=" + serviceDes + ", data=" + data
                    + "]";
        }

    }
}
